/**
 *Author: chenxiaoyu
 *Date: 2018/4/2
 *Email: 1524904743@qq.com
 *
 */

var mainModule= angular.module('mainModule', []);

/**
 * 头部controller
 *
 */
mainModule.controller('headerCtrl', [
    '$scope',
    '$state',
    '$location',
    function ($scope, $state, $location) {
        $scope.pageurl= $location.path();

        init();
        function init() {
            $scope.$on('$locationChangeSuccess', function (event, newUrl, oldUrl) {
                $scope.pageurl= $location.path();
            })
        };

        // 刷新首页
        $scope.reloadIndex= function () {
            $state.go('index');
        };

        // 管理中心样式
        $scope.activeId= -1;
        $scope.boxStyle= {left: '0px'};
        $scope.managerData= [{name: '知识管理', data: [{name:'知识上报', id: 1, url: './client/components/managerCenter/template/knowledgeReport.html'}, {name:'问题配置', id: 2}], id: 1}, {name: '标签管理', data: [{name:'常规标签管理', id: 1}, {name:'个性标签管理', id: 2}], id: 1}, {name: '知识管理', data: [], id: 1},
            {name: '知识管理', data: [], id: 1}, {name: '知识管理', data: [], id: 1}]

        $scope.openManager= function (d, i) {
            $scope.boxStyle= {left: i * 140 + 'px'};
            $scope.activeId= i;
            $scope.managerDataList= d;
        }
        
        $scope.closeManager= function () {
            $scope.boxStyle= {left: '0px'};
            $scope.activeId= -1;
            $scope.managerDataList= [];
        }
        
        // 打开相关选项卡
        $scope.openTabs= function (i) {
            $scope.$emit('managerOpenTabs', $scope.managerDataList[i]);
        }
    }
]);

/*
* 首页主体controller
* */
mainModule.controller('mainCtrl', [
    '$scope',
    '$timeout',
    '$state',
    function ($scope, $timeout, $state) {
        $scope.listData= [{name:'测试业务1'},{name:'测试业务2'},{name:'测试业务3'}];
        $scope.showId= -1;
        $scope.loc= 0;
        $scope.tabShow= false;
        $scope.searchMark= '';
        // 选项卡数据
        $scope.tabDatas= [],

        // 左侧列表事件处理
        // 鼠标移入事件
        $scope.showWindow= function (id, e) {
            $scope.showId= id;
        }
        // 鼠标离开事件
        $scope.hideWindow= function () {
            $scope.showId= -1;
        }
        // 鼠标点击事件
        $scope.showMenu= 0;
        $scope.openDetail= function (e, index) {
            var tabObj= {name: '测试业务1', url: './client/components/index/template/search.html?type=menu&ses=基金', id:1};
            var thisObj= angular.element(e.target);
            if (thisObj[0].nodeName.toLowerCase() === 'li' || (thisObj[0].nodeName.toLowerCase() === 'span' && thisObj.parent()[0].nodeName.toLowerCase() == 'li')){
                var parentObj= thisObj.parents('div.left-menu');
                var rightObj= parentObj.next('div.right-cotent');
                parentObj.css('height', '75px');
                rightObj.css('overflow', 'hidden').css('height', '75px');
                $scope.tabShow= true;
                $scope.showMenu= index;
                $scope.tabDatas.push(tabObj);
            }else {
                alert(thisObj[0].nodeName);
            }
        }

        // 主页搜索
        $scope.isClear= false;
        $scope.searchs= function () {
            angular.element('div.left-menu').css('height', '75px');
            angular.element('div.right-cotent').css('overflow', 'hidden').css('height', '75px');
            $scope.tabShow= true;
            $scope.tabDatas.push({name: '关键字检索-' + $scope.searchMark,id:2, url: './client/components/index/template/search.html?type=input&ses=黄金'});
        }

        // 监听输入内容，显示清除按钮
        $scope.$watch('searchMark', function (newVal, oldVal) {
            if (!!newVal){
                $scope.isClear= true;
            }else {
                $scope.isClear= false;
            }
        })

        $scope.clearSearchMark= function () {
            $scope.searchMark= '';
        }

        $scope.capData= {
            title: '更多最新知识',
            data: ['系列', '任务编号', '任务类型', '任务主题', '紧急程度', '提交人/行政机构', '提交时间', '操作'],
            pager: {
                pageNum: 1,
                pageSize: 1,
                totalCount: 5
            },
            changePage: function (cp, event) {
                this.pager.pageNum= cp;
            }
        };

        // 打开更多弹窗
        $scope.openMoreList= function (t) {
            var obj= {title: '更多最新知识'};
            switch(t){
                case 'new':
                    obj.title= '更多最新知识';
                    console.log('new');
                    break;
                case 'per':
                    console.log('per');
                    return;
                    break;
                case 'hot':
                    obj.title= '更过热门知识';
                    console.log('hot');
                    break;
                case 'col':
                    obj.title= '收藏夹'
                    console.log('col');
                    break;
                default: ;
            }
            angular.extend($scope.capData, obj);
            $scope.winShow= true;
        }

        // 无序列表数据
        $scope.newData= [
            {id: 1, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 2, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 3, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 4, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 5, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 6, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 7, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 8, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 9, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 10, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 11, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 12, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 13, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 14, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 15, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 16, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 17, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 18, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 19, title: '故事点怎么估算？[故事点]', date: '2018-4-14'},
            {id: 20, title: '故事点怎么估算？[故事点]', date: '2018-4-14'}
        ]
    }
])

/*
* 首页选项卡主页
*
* */
mainModule.controller('indexTabPageCtrl', [
    '$scope',
    function ($scope) {
        init();

        function init(){
            $scope.paramObj= {};
            $scope.pageContent= '';
            var paramStr= $scope.pageUrl.substring($scope.pageUrl.lastIndexOf('?')+1);
            var paramArr= paramStr.split('&');
            angular.forEach(paramArr, function (item) {
                $scope.paramObj[item.substring(0, item.indexOf('='))]= item.substring(item.indexOf('=')+1);
            })

            if ($scope.paramObj.type === 'menu'){
                $scope.pageContent= '基金是一种非常安全的理财方式=' + new Date().getTime();
            }else if ($scope.paramObj.type === 'input') {
                $scope.pageContent= '黄金是贵金属投资中非常常见的一种金属=' +  + new Date().getTime();
            }
        }
    }
])
